//
//  SEGSecurityManager.m
//  SEGBasic
//
//  Created by Clavis on 2019/5/24.
//  Copyright © 2019 刘山. All rights reserved.
//

#import "SEGSecurityManager.h"

#define kSEGISecurityVersionKey @"SEGI_Security_Version_Key"

@interface SEGSecurityManager ()

@property (nonatomic, strong) NSMutableDictionary *loginNeedShowAuthViewDictionary;
@property (nonatomic, strong) NSMutableDictionary *modifyPasswordNeedShowAuthViewDictionary;

@end

@implementation SEGSecurityManager

static SEGSecurityManager * _instance;

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!_instance) {
            _instance = [super allocWithZone:zone];
        }
    });
    return _instance;
}

+ (instancetype)shared {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!_instance) {
            _instance = [[self alloc] init];
        }
    });
    return _instance;
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
    return self;
}

+ (BOOL)isAgreeSecurity {
    // 本地已经同意的安全政策版本
    NSInteger localSecurityVersion = [[[NSUserDefaults standardUserDefaults] objectForKey:kSEGISecurityVersionKey] integerValue];
    return localSecurityVersion == [SEGSecurityManager shared].securityVersion;
}

+ (void)updateLocalSecurityVersion {
    [[NSUserDefaults standardUserDefaults] setObject:@([SEGSecurityManager shared].securityVersion) forKey:kSEGISecurityVersionKey];
}

- (void)setLoginNeedShowAuthView:(NSString *)tel status:(BOOL)status {
    if (tel && tel.length > 0) {
        [self.loginNeedShowAuthViewDictionary setObject:@(status) forKey:tel];
    }
}

- (void)setModifyPasswordNeedShowAuthView:(NSString *)tel status:(BOOL)status {
    if (tel && tel.length > 0) {
        [self.modifyPasswordNeedShowAuthViewDictionary setObject:@(status) forKey:tel];
    }
}

- (BOOL)getLoginNeedShowAuthView:(NSString *)tel {
    if (tel && tel.length > 0) {
        return [[self.loginNeedShowAuthViewDictionary objectForKey:tel] boolValue];
    }
    return NO;
}

- (BOOL)getModifyNeedShowAuthView:(NSString *)tel {
    if (tel && tel.length > 0) {
        return [[self.modifyPasswordNeedShowAuthViewDictionary objectForKey:tel] boolValue];
    }
    return NO;
}


#pragma mark - Getter Setter
- (NSDictionary *)loginNeedShowAuthViewDictionary {
    if (!_loginNeedShowAuthViewDictionary) {
        _loginNeedShowAuthViewDictionary = [[NSMutableDictionary alloc] init];
    }
    return _loginNeedShowAuthViewDictionary;
}

- (NSMutableDictionary *)modifyPasswordNeedShowAuthViewDictionary {
    if (!_modifyPasswordNeedShowAuthViewDictionary) {
        _modifyPasswordNeedShowAuthViewDictionary = [[NSMutableDictionary alloc] init];
    }
    return _modifyPasswordNeedShowAuthViewDictionary;
}


#pragma mark - 越狱检测
// 判断是否u越狱
- (BOOL)isJailBreak {
    BOOL isFileExist = [self isJailBreakByFileExist];
    BOOL isCanOpenCydia = [self isJailBreakByCanOpenCydia];
    BOOL isCanObtainApplications = [self isJailBreakByJudgeObtainApplications];
    return (isFileExist || isCanOpenCydia || isCanObtainApplications);
}


// 判断这些文件是否存在
- (BOOL)isJailBreakByFileExist  {
    NSArray *jailbreak_tool_paths = @[
        @"/Applications/Cydia.app",
        @"/Library/MobileSubstrate/MobileSubstrate.dylib",
        @"/bin/bash",
        @"/usr/sbin/sshd",
        @"/etc/apt"
    ];
    
    for (int i=0; i<jailbreak_tool_paths.count; i++) {
        if ([[NSFileManager defaultManager] fileExistsAtPath:jailbreak_tool_paths[i]]) {
            NSLog(@"The device is jail broken!");
            return YES;
        }
    }
    NSLog(@"The device is NOT jail broken!");
    return NO;
}

// 根据是否能打开cydia判断
- (BOOL)isJailBreakByCanOpenCydia {
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"cydia://"]]) {
        NSLog(@"The device is jail broken!");
        return YES;
    }
    NSLog(@"The device is NOT jail broken!");
    return NO;
}

// 是否能获取所有应用的名称判断
- (BOOL)isJailBreakByJudgeObtainApplications {
    if ([[NSFileManager defaultManager] fileExistsAtPath:@"User/Applications/"]) {
        NSLog(@"The device is jail broken!");
        NSArray *appList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"User/Applications/" error:nil];
        NSLog(@"appList = %@", appList);
        return YES;
    }
    NSLog(@"The device is NOT jail broken!");
    return NO;
}

@end
